package com.agg.persist.ast;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.util.JdbcConstants;

import java.util.List;

public class DruidAst {
    public static void main(String[] args) {
        String sql="select * from  t1 inner join t2 on t1.id=t2.id";
        String dbType = JdbcConstants.MYSQL;
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        String tt=SQLUtils.addSelectItem(sql,"t3","tt",dbType);
        tt= SQLUtils.addCondition(tt,"t=t",null);
       // System.out.println(tt);
        String condtion=getEqExpr("productName","product_name","t1").toString();
       tt= SQLUtils.addCondition(tt,condtion,SQLBinaryOperator.BooleanAnd,false,dbType);
        System.out.println(tt);
    }

    private static SQLBinaryOpExpr getEqExpr(String name,String column, String group){
        SQLBinaryOpExpr opExpr=new SQLBinaryOpExpr();
        opExpr.setOperator(SQLBinaryOperator.Equality);
        SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr(group);
        SQLPropertyExpr left = new SQLPropertyExpr(sqlIdentifierExpr, column);
        sqlIdentifierExpr.setParent(left);
        opExpr.setLeft(left);
        SQLIdentifierExpr right = new SQLIdentifierExpr ("#{cm."+name+"}");
        opExpr.setRight(right);
        return opExpr;
    }

}
